package com.cskaoyan.controller.wx;

import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.cart.Cart;
import com.cskaoyan.bo.cartBo.CartCheckedBo;
import com.cskaoyan.bo.cartBo.CartDeleteBo;
import com.cskaoyan.service.wx.CartService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.wx.CartListVo;
import com.cskaoyan.vo.wx.CheckOut_FlagVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    CartService cartService;

    @RequestMapping("index")
    public BaseRespVo index(){
        CartListVo cartListVo = cartService.index(MyUtils.getUserId());
        return BaseRespVo.ok(cartListVo);
    }

    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedBo cartCheckedBo){
        CartListVo cartListVo = cartService.checked(cartCheckedBo,MyUtils.getUserId());
        return BaseRespVo.ok(cartListVo);
    }

    @RequestMapping("add")
    public BaseRespVo add(@RequestBody Cart cart){
        cart.setUserId(MyUtils.getUserId());
        long total = cartService.add(cart);
        return BaseRespVo.ok(total);
    }

    @RequestMapping("goodscount")
    public BaseRespVo goodsCount(){
        long total = cartService.goodsCount(MyUtils.getUserId());
        return BaseRespVo.ok(total);
    }

    @RequestMapping("update")
//    public BaseRespVo update(@RequestBody CartUpdateBo cartUpdateBo){
    //这里尝试用Cart来接收数据
    public BaseRespVo update(@RequestBody Cart cart){
        int status = cartService.update(cart);
        if(status != 0){
            String num = Integer.toString(status);
            return BaseRespVo.failStatus(-1,"商品数量：" + num);
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CartDeleteBo cartDeleteBo){
        CartListVo cartListVo = cartService.delete(cartDeleteBo);
        return BaseRespVo.ok(cartListVo);
    }

    @RequestMapping("fastadd")//这里同时实现了向购物车中添加商品
    public BaseRespVo fastAdd(@RequestBody Cart cart){
        cart.setUserId(MyUtils.getUserId());
        long cartId = cartService.fastAdd(cart);
        return BaseRespVo.ok(cartId);
    }

    @RequestMapping("checkout")
    public BaseRespVo checkout(Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId){

        CheckOut_FlagVo checkOutFlagVo = cartService.checkOut(cartId,addressId,couponId,grouponRulesId,MyUtils.getUserId());
        //这里应该不会报空指针异常
        if(checkOutFlagVo.getMessage() != null){
            return BaseRespVo.failStatus(-1,checkOutFlagVo.getMessage());
        }
        return BaseRespVo.ok(checkOutFlagVo.getCheckOutVo());
    }
}
